package com.ty.zls.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.SearchView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ty.zls.R;

/**
 * @author Administrator
 * @createTime 2019/12/5 20:19
 */
public class MySearchView extends RelativeLayout {
    private final Context mContext;
    private onComplete onComplete;

    public void setOnComplete(MySearchView.onComplete onComplete) {
        this.onComplete = onComplete;
    }

    public MySearchView(Context context) {
        super(context);
        this.mContext = context;
    }

    public MySearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        TypedArray attributes = mContext.obtainStyledAttributes(attrs, R.styleable.MySearchView);
        String mTitle = attributes.getString(R.styleable.MySearchView_title);

        View view = LayoutInflater.from(mContext).inflate(R.layout.view_search, null);
        addView(view);
        SearchView mSearchView = (android.support.v7.widget.SearchView) view.findViewById(R.id.search_view);
        TextView mTvTitle = (TextView) view.findViewById(R.id.tv_title);
        mTvTitle.setText(mTitle == null ? "" : mTitle);
        mSearchView.onActionViewExpanded();
        mSearchView.clearFocus();
        mSearchView.setOnQueryTextListener(new android.support.v7.widget.SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                if (onComplete != null) {
                    onComplete.onCompleteInput(query);
                }
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                if (onComplete != null) {
                    onComplete.onQueryTextChangeStr(newText);
                }
                return false;
            }
        });
    }

    public interface onComplete {
        void onCompleteInput(String str);

        void onQueryTextChangeStr(String str);
    }
}
